//导入数据库操作类
const myDb = require('../../../libs/mydb');
const { md5 } = require('../../../utiles/mycrypto');

//导入加密操作类
const myCrypto = require('../../../utiles/mycrypto')

//时间
const time = require('../../../utiles/time')

module.exports = {

    /**
     * 查询管理员列表渲染
     * @param {*} req 
     * @param {*} res 
     * @returns 
     */
    check_admin: async function (req, res) {


        let amdinData;
        await myDb.singleSearch('ac_admin,ac_role,ac_power',
            'ac_admin.a_id,ac_admin.a_name,ac_admin.a_password,ac_admin.a_last_entertime,ac_role.role_name,ac_power.p_name,ac_power.p_parent_id'
            , 'WHERE ac_admin.a_role_id = ac_role.role_id AND ac_role.role_id = ac_power.p_id ')
            .then(
                result => {
                    amdinData = result;
                }
            )
        return amdinData
    },
    /**
     * 查询用户列表渲染
     * @param {*} req 
     * @param {*} res 
     * @returns 
     */
    check_user: async function (req, res) {
        let userData;
        await myDb.singleSearch('ac_user', 'u_id,u_name,u_password,u_tele,u_reg_time,u_last_log', '')
            .then(
                result => {
                    userData = result
                }
            )
        return userData;
    },

    /**
     * 文章列表查询
     * @param {*} req 
     * @param {*} res 
     */

    check_text: async function (req, res) {
        let textData;
        await myDb.singleSearch('ac_hot_texts,ac_user', 'tid,uid,release_time,content_text,u_name',
            ' WHERE ac_hot_texts.uid=ac_user.u_id')
            .then(
                result => {
                    textData = result
                }
            )
        return textData;
    },
    /**
     * 获取评论列表
     * @param {*} req 
     * @param {*} res 
     * @returns 
     */
    check_comment: async function (req, res) {
        let data;
        await myDb.singleSearch('ac_comment,ac_user', 'com_id,text_id,uid,com_text,com_time,u_name',
            ' WHERE ac_comment.uid=ac_user.u_id')
            .then(
                result => {
                    data = result
                }
            )
        return data;
    },

    //删除管理员信息
    del_admin: function (req, res) {
        let { aname } = req.body;
        myDb.del('ac_admin', `a_name='${aname}'`)
            .then(
                result => {
                    console.log(result);
                    res.send({
                        code: 200,
                        msg: 'Delete succeeded'
                    })
                }
            )
    },

    //修改管理员信息
    modify_admin: function (req, res) {
        let { aname, data } = req.body;
        myDb.modify('ac_admin', `${data}'`, `WHERE a_name=${aname}`)
            .then(
                result => {
                    console.log(result);
                    res.send({
                        code: 200,
                        msg: 'Modify succeeded'
                    })
                }
            )
    },

    //添加管理员
    add_admin: async function (req, res) {
        let data = JSON.parse(req.body.data);
        let newTime = time(Date.now())
        console.log(newTime)
        for (let x in data) {
            if (data[x] == '') {
                res.send({
                    code: 400,
                    msg: 'Data cannot be empty'
                });
            }
        }

        await myDb.singleSearch('ac_admin', 'a_name', 'WHERE a_name IS NOT NULL')
            .then(
                result => {
                    result.forEach(element => {
                        if (element.a_name == data.a_name) {
                            res.send({
                                code: 401,
                                msg: 'repeat of adminname'
                            });
                        }
                    });
                }
            )

        await myDb.singleSearch('ac_role', 'role_id', `WHERE role_name='${data.a_role_id}'`)
            .then(
                result => {
                    result.forEach(element => {
                        let dataA = {
                            "a_name": data.a_name,
                            "a_password": data.a_password,
                            "a_role_id": element.role_id,
                            "a_last_entertime": newTime
                        }
                        myDb.add('ac_admin', dataA, '')
                            .then(
                                result => {
                                    res.send({
                                        code: 200,
                                        msg: 'success'
                                    });
                                }
                            )
                    });
                }
            )


    },

    /**
     * 用户密码重置
     * @param {*} req 
     * @param {*} res 
     */
    rest_userpassword: function (req, res) {

        let uid = req.body.uid;
        myDb.modify('ac_user', { 'u_password': md5('123456') }, ` WHERE u_id=${uid}`)
            .then(
                result => {
                    if (result) {
                        res.send({
                            code: 200,
                            msg: 'ok'
                        })
                    } else {
                        res.send({
                            code: 400,
                            msg: 'Upassword REST is SUCCESSED'
                        })
                    }
                }
            )
    },


    /**
     * 管理员密码修改
     * @param {*} req 
     * @param {*} res 
     */

    res_adminpawd: function (req, res) {


        myDb.modify('ac_admin', { 'a_password': md5(req.body.newpassword) }, ` WHERE a_id=${req.body.aid}`)
            .then(
                result => {
                    if (result) {
                        res.send({
                            code: 200,
                            msg: 'Password REST SUCCESSED'
                        })
                    } else {
                        res.send({
                            code: 400,
                            msg: "Password not REST Success"
                        })
                    }
                }
            )
    },

    /**
     * 文章删除
     * @param {*} req 
     * @param {*} res 
     */
    delet_text: async function (req, res) {

        await myDb.singleSearch('ac_comment', 'com_id', ` WHERE text_id='${req.body.tid}'`)
            .then(
                result => {
                    if (result.length == 0) {
                        myDb.del('ac_hot_texts', ` WHERE ac_hot_texts.tid=${req.body.tid}`)
                            .then(
                                result => {
                                    if (result) {
                                        res.send({
                                            code: 200,
                                            msg: 'OK'
                                        })
                                    } else {
                                        res.send({
                                            code: 400,
                                            msg: "Delete is NOt SUCCESS"
                                        })
                                    }
                                }
                            )

                    } else {
                        myDb.moreDel('ac_hot_texts,ac_comment',
                            ` WHERE ac_hot_texts.tid=${req.body.tid} 
                        AND ac_comment.text_id=${req.body.tid}`)
                            .then(
                                result => {
                                    if (result) {
                                        res.send({
                                            code: 200,
                                            msg: 'OK'
                                        })
                                    } else {
                                        res.send({
                                            code: 400,
                                            msg: "Delete is NOt SUCCESS"
                                        })
                                    }
                                }
                            )


                    }
                }
            )
    },

    /**
     * 评论删除
     * @param {*} req 
     * @param {*} res 
     */

    delet_com: function (req, res) {
        myDb.del('ac_comment', ` WHERE com_id=${req.body.cid}`)
            .then(
                result => {
                    if (result) {
                        res.send({
                            code: 200,
                            msg: "ok"
                        })
                    } else {
                        res.send({
                            code: 400,
                            msg: 'NO Delete'
                        })
                    }
                }
            )
    }

}